<template>
	<view>
		<navigationbar :leftIcon="leftIcon" :title="title" />
		<view v-if="userInfo" class="userPageTop">
			<view class="left" @click="userDetail">
				<text class="userName text">{{ userInfo.nickname }}</text>
				<text class="text">
					<text class="iconfont icon-bianji"></text>
					编辑我的信息
				</text>
			</view>
			<view class="right"><image :src="userInfo.avatar"></image></view>
		</view>
		<view v-else class="userPageTop" @click="login">
			<view class="left">
				<text class="userName text">未登录/注册</text>
				<text class="text">点击此处登录/注册</text>
			</view>
		</view>
		<view class="userPageRow">
			<view class="item" @click="userDetail">
				<image src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-1f24ae54-f413-4707-aa3f-f365584e95cd/e7f95c43-c48d-4c43-be88-b269a89c9436.png"></image>
				<text>我的信息</text>
			</view>
			<view class="item" @click="uploadFile">
				<image src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-1f24ae54-f413-4707-aa3f-f365584e95cd/15c62011-9446-4a64-b82e-d6cfe6acc53e.png"></image>
				<text>附件简历</text>
			</view>
			<view class="item" @click="toIntention">
				<image src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-1f24ae54-f413-4707-aa3f-f365584e95cd/cc8f9481-882c-4182-b45a-fd00070b8e17.png"></image>
				<text>求职意向</text>
			</view>
			<view class="item" @click="toPostMsg">
				<image src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-1f24ae54-f413-4707-aa3f-f365584e95cd/76207457-0e11-46c0-9038-4887ce7154f4.png"></image>
				<text>应聘消息</text>
			</view>
		</view>
		<view class="list">
			<u-cell-group>
				<u-cell size="large" icon="share" title="我的收藏" :isLink="true" @click="myCollection"></u-cell>
				<u-cell size="large" icon="map" title="我的招聘" :isLink="true" @click="myRecruitment"></u-cell>
				<u-cell size="large" icon="map" title="反馈建议" :isLink="true" @click="toFankui"></u-cell>
			</u-cell-group>
		</view>
		<view class="quitContainer" v-if="userInfo">
			<view class="userPageBottom" @click="quitLogin"><u-button text="退出登录" size="normal" :color="bgColor" shape="circle"></u-button></view>
		</view>
		<view>
			<u-modal @confirm="confirm" @cancel="cancel" :show="modalShow" :title="modalTitle" :showCancelButton="true"></u-modal>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
import navigationbar from '../navigationBar.vue';
export default {
	components: {
		navigationbar
	},
	data() {
		return {
			userInfo: '',
			// userInfo: {
			// 	nickname: '佩奇',
			// 	avatar: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-1f24ae54-f413-4707-aa3f-f365584e95cd/bb4e95a7-3c2d-45e1-a6fb-f1341d2d05c0.jpg'
			// },
			leftIcon: '',
			title: 'My',
			modalTitle: '确定退出登录?',
			modalShow: false,
			bgColor: '#7aaffe'
		};
	},
	onLoad() {
		uni.getStorage({
			key: 'user_id',
			success: (res) => {
				let that = this
				let id = res.data
				uniCloud.callFunction({
					name: 'getUserInfo',
					data: {
						id
					},
					success(data) {
						that.userInfo = data.result.data
						that.$store.state.userInfo = data.result.data
						console.log(that.userInfo)
					}
				})
			}
		})
	},
	onShow() {
		this.userInfo = this.$store.state.userInfo
	},
	methods: {
		// 附件简历
		uploadFile() {
			if(!this.$store.state.userID){
				this.$refs.uToast.show({
					message: '请先登录',
					icon: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
					type: 'error'
				});
				return
			}
			uni.navigateTo({
				url: '../userResume/userResume'
			});
		},
		// 职位意向
		toIntention() {
			if(!this.$store.state.userID){
				this.$refs.uToast.show({
					message: '请先登录',
					icon: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
					type: 'error'
				});
				return
			}
			this.$store.commit('setIntentionKey', true)
			uni.navigateTo({
				url: 'intention/intention'
			})
		},
		toPostMsg() {
			if(!this.$store.state.userID){
				this.$refs.uToast.show({
					message: '请先登录',
					icon: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
					type: 'error'
				});
				return
			}
			uni.navigateTo({
				url: '../message/postMessage/postMessage'
			})
		},
		toFankui(){
			uni.navigateTo({
				url:'../fankui/fankui'
			})
		},
		login() {
			let that = this
			uni.getUserProfile({
				desc: '获取个人信息',
				success: loginRes => {
					let { nickName, avatarUrl } = loginRes.userInfo;
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							uniCloud
								.callFunction({
									name: 'getWXopenid',
									data: {
										code: loginRes.code
									}
								})
								.then(res => {
									let { openid, session_key } = res.result;
									uniCloud.callFunction({
										name: 'userLogin',
										data: {
											openid,
											nickName,
											avatarUrl
										},
										success(res) {
											console.log(res);
											let { result } = res;
											that.userInfo = {
												nickname: result.data.nickname || nickName,
												avatar: result.data.avatar || avatarUrl
											}
											uni.setStorage({
												key: 'user_id',
												data: result.data.id || result.data._id
											});
											that.$store.commit('setUserID', result.data.id || result.data._id)
											that.$store.commit('setUserInfo', result.data)
										}
									});
								});
						}
					});
				}
			});
		},
		quitLogin() {
			this.modalShow = true
		},
		cancel() {
			this.modalShow = false
		},
		confirm() {
			this.modalShow = false
			this.userInfo = ''
			uni.removeStorage({
				key: 'user_id',
				success: (res) => {
					this.$store.commit('setUserID', '')
					this.$store.commit('setUserInfo', '')
				}
			});
		},
		// 我的收藏
		myCollection() {
			if(!this.$store.state.userID){
				this.$refs.uToast.show({
					message: '请先登录',
					icon: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
					type: 'error'
				});
				return
			}
			uni.navigateTo({
				url: '../collectionList/collectionList'
			})
		},
		// 我的招聘
		myRecruitment() {
			if(!this.$store.state.userID){
				this.$refs.uToast.show({
					message: '请先登录',
					icon: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
					type: 'error'
				});
				return
			}
			uni.navigateTo({
				url: '../myRecruitment/myRecruitment'
			})
		},
		userDetail() {
			if(!this.$store.state.userID){
				this.$refs.uToast.show({
					message: '请先登录',
					icon: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
					type: 'error'
				});
				return
			}
			uni.navigateTo({
				url: '../onlineInfo/onlineInfo'
			})
		}
	}
};
</script>

<style lang="scss">
.icon-bianji {
	margin-right: 10rpx;
}
.userPageTop {
	height: 200rpx;
	padding-top: 20rpx;
	background-color: #7aaffe;
	.left {
		float: left;
		margin-top: 20rpx;
		.text {
			display: block;
			font-size: 30rpx;
			margin-left: 45rpx;
		}
		.userName {
			font-size: 50rpx;
			margin-bottom: 15rpx;
		}
	}
	.right {
		float: right;
		width: 150rpx;
		height: 150rpx;
		margin-right: 35rpx;
		image {
			width: 150rpx;
			height: 150rpx;
			border-radius: 75rpx;
		}
	}
}
.userPageRow {
	height: 200rpx;
	width: 97%;
	margin: auto;
	display: flex;
	flex-flow: row;
	justify-content: space-around;
	align-items: center;
	.item {
		font-size: 25rpx;
		image {
			width: 90rpx;
			height: 90rpx;
			display: block;
			margin: 10rpx auto;
		}
	}
}
.list {
	margin-top: 20rpx;
}
.quitContainer {
	width: 100%;
	position: absolute;
	bottom: 60rpx;
	.userPageBottom {
		width: 60%;
		margin: 0 auto;
	}
}
</style>
